package com.monboo.album.view
{
	/**
	 * @author robin for Tudou AlbumPlayer 2010-03-26
	 * @FullScreenMediator FullScreen
	 * @email:rman@live.cn
	 * @blog:www.xncat.com
	 */
	
	import com.gs.TweenLite;
	import com.monboo.album.command.AlbumEvent;
	
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class FullScreenMediator  extends Mediator
	{
		public static const NAME:String = "FullScreenMediator"; 
		
		private var hideMap:Array = new Array();
		private var parkMap:Array = new Array();
		private var showMap:Array = new Array();
		
		private var _stage:Stage;
		private var viewObj:AlbumLite;
		private var menuInState:Boolean = false;
		private var fullData:Boolean = false;
		
		public function FullScreenMediator(obj:Object)
		{
			super(NAME, obj); 
			viewObj = obj as AlbumLite;
			_stage = viewObj.stage;
		}
		override public function listNotificationInterests():Array 
        {  
            return [ 
            		 AlbumEvent.CHANGE_UI,
            		 AlbumEvent.RESET_UI
            ];
        }
		 override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
            {  	                
           		case AlbumEvent.CHANGE_UI:
                	changeUI();
                	break;
                case AlbumEvent.RESET_UI:
                	resetUI();
                	break;
            }
        }
        public function registShow(arr:Array):void{
        	for(var i:int=0;i<arr.length;i++){
				hideMap.showMap(arr[i]);
				arr[i].visible = false;
			}
        }
        public function registHide(arr:Array):void{
			for(var i:int=0;i<arr.length;i++){
				hideMap.push(arr[i]);
			}
		}
		public function registPark(arr:Array):void{
			for(var i:int=0;i<arr.length;i++){
				parkMap.push(arr[i]);
				arr[i]["normalPosition"] = new Point(arr[i]["target"].x,arr[i]["target"].y);
			}
		}
		
        private function changeUI():void{
			hide(true);
			show(true);
			_stage.addEventListener(MouseEvent.MOUSE_MOVE,showFullscreenMenu);
			
			if(!fullData){initFullData();fullData = true;}
			
			for(var i:int=0;i<parkMap.length;i++){
				parkMap[i]["target"].x = parkMap[i]["fullPosition"].x;
				parkMap[i]["target"].y = parkMap[i]["fullPosition"].y;
			}
		}
		private function resetUI():void{
			hide(false);
			show(false);
			menuInState = false;
			_stage.removeEventListener(MouseEvent.MOUSE_MOVE,showFullscreenMenu);
			
			TweenLite.killDelayedCallsTo(hideFullscreenMenu);
			
			for(var i:int=0;i<parkMap.length;i++){
				TweenLite.killTweensOf(parkMap[i]["target"]);
				
				parkMap[i]["target"].x = parkMap[i]["normalPosition"].x;
				parkMap[i]["target"].y = parkMap[i]["normalPosition"].y;
			}	
		}
	
		private function showFullscreenMenu(e:MouseEvent):void{
			if(!menuInState){
				menuInState = true;
				if (parkMap.length<=0) return;
				for(var i:int=0;i<parkMap.length;i++){
					if(parkMap[i]["align"] == "top" || parkMap[i]["align"] == "bottom"){
						TweenLite.to(parkMap[i]["target"],0.5,{y:parkMap[i]["menuIn"]});
					}else{
						TweenLite.to(parkMap[i]["target"],0.5,{x:parkMap[i]["menuIn"]});
					}	
				}	
				
			}
			TweenLite.killDelayedCallsTo(hideFullscreenMenu);	
			TweenLite.delayedCall(4,hideFullscreenMenu);
		}
		private function hideFullscreenMenu():void{
			
			menuInState = false;
			if (parkMap.length<=0) return;
			for(var i:int=0;i<parkMap.length;i++){
				stayPark(parkMap[i],parkMap[i]["menuOut"]);
			}
		}
		private function stayPark(parkChild:Object,menuOut:Number):void{
			var target:Object = parkChild["target"];
			var mX:Number = _stage.mouseX;
			var mY:Number = _stage.mouseY;
			if(mY<target.y ||mY>target.y+target.height){
				if(parkChild["align"] == "top" || parkChild["align"] == "bottom"){
					TweenLite.to(target,1,{y:menuOut});
				}else{
					TweenLite.to(target,1,{x:menuOut});
				}
				
			}else if(mX<target.x ||mX>target.x+target.width){
				if(parkChild["align"] == "top" || parkChild["align"] == "bottom"){
					TweenLite.to(target,1,{y:menuOut});
				}else{
					TweenLite.to(target,1,{x:menuOut});
				}
			}
		}
		
		private function show(state:Boolean):void{
			if (showMap.length<=0) return;
			for(var i:int=0;i<showMap.length;i++){
				if(state){
					showMap[i].visible = true;
				}else{
					showMap[i].visible = false;
				}
			}
		}
		private function hide(state:Boolean):void{
			if (hideMap.length<=0) return;
			for(var i:int=0;i<hideMap.length;i++){
				if(state){
					hideMap[i].visible = false;
				}else{
					hideMap[i].visible = true;
				}
			}
		}
		
		private function initFullData():void{
			for(var i:int=0;i<parkMap.length;i++){
				switch (parkMap[i]["align"]){
					case "top":
						parkMap[i]["fullPosition"] = new Point(transOffset(parkMap[i]),parkMap[i]["pad"]);
						parkMap[i]["menuIn"] = parkMap[i]["pad"];
						parkMap[i]["menuOut"] = 0 - parkMap[i]["target"].height*2;
						break;
					case "right":
						parkMap[i]["fullPosition"] = new Point(_stage.stageWidth-parkMap[i]["target"].width-parkMap[i]["pad"],transOffset(parkMap[i]));
						parkMap[i]["menuIn"] = _stage.stageWidth-parkMap[i]["target"].width-parkMap[i]["pad"];
						parkMap[i]["menuOut"] = _stage.stageWidth + parkMap[i]["target"].width*2;
						break;
					case "bottom":
						parkMap[i]["fullPosition"] = new Point(transOffset(parkMap[i]),_stage.stageHeight-parkMap[i]["target"].height-parkMap[i]["pad"]);
						parkMap[i]["menuIn"] = _stage.stageHeight-parkMap[i]["target"].height-parkMap[i]["pad"];
						parkMap[i]["menuOut"] = _stage.stageHeight + parkMap[i]["target"].height*2;
						break;
					case "left":
						parkMap[i]["fullPosition"] = new Point(parkMap[i]["pad"],transOffset(parkMap[i]));
						parkMap[i]["menuIn"] = parkMap[i]["pad"];
						parkMap[i]["menuOut"] = 0 - parkMap[i]["target"].width*2;
						break;
				}		
			}
		}
		
		private function transOffset(park:Object):Number{
			var offset:Number;
			if(park["align"] == "top" || park["align"] == "bottom"){
				if(isNaN(park["offset"])){
					if(park["offset"]=="left"){
						offset = 0;
					}else if(park["offset"]=="center"){
						offset = (_stage.stageWidth-park["target"].width)/2;
					}else if(park["offset"]=="right"){
						offset = _stage.stageWidth-park["target"].width;
					}
				}else{
					offset = park["offset"];
				}
			}else if(park["align"] == "right" || park["align"] == "left"){
				if(isNaN(park["offset"])){
					if(park["offset"]=="top"){
						offset = 0;
					}else if(park["offset"]=="center"){
						offset = (_stage.stageHeight-park["target"].height)/2;
					}else if(park["offset"]=="bottom"){
						offset = _stage.stageHeight-park["target"].height; 
					}
				}else{
					offset = park["offset"];
				}
			}
			return offset;
		}
	}
}